<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="配置名称" prop="name">
        <el-input class="w500" v-model="form.name" placeholder="例如：配置名称"></el-input>
      </el-form-item>
      <el-form-item label="配置分类">
        <el-cascader class="w500" clearable v-model="cateIds" :options="tree" :props="defaultProps" change-on-select>
        </el-cascader>
      </el-form-item>
      <el-form-item label="值">
        <el-input class="w500" type="textarea" v-model="form.value" placeholder="若为checkbox多个值 则用逗号分隔"></el-input>
      </el-form-item>
      <el-form-item label="备选项">
        <el-input class="w500" type="textarea" v-model="form.other_info"
                  placeholder="当type为radio checkbox时，存放多种选择。而value为值"></el-input>
      </el-form-item>
      <el-form-item label="识别码" prop="action">
        <el-input class="w500" v-model="form.action"placeholder="例如：logo"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-radio-group v-model="form.type">
          <el-radio label="1">单选</el-radio>
          <el-radio label="2">多选</el-radio>
          <el-radio label="3">文本域</el-radio>
          <!--<el-radio label="4" :disabled="true">图片</el-radio>-->
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序">
        <el-input class="w500" v-model="form.order" type="number"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {mapState} from 'vuex'
  import notify from '@/tools/notify'

  export default {
    data () {
      return {
        form: {
          category_id: '',
          name: '',
          value: '',
          other_info: '',
          action: '',
          type: '1',
          order: 1,
          status: '1'
        },
        rules: {
          name: [
            {required: true, message: '请输入配置名称', trigger: 'blur'}
          ],
          action: [
            {required: true, message: '请输入识别码', trigger: 'blur'}
          ]
        },
        defaultProps: {
          value: 'id',
          label: 'name',
          children: '_child'
        },
        cateIds: []
      }
    },
    computed: {
      ...mapState({
        tree: state => state.setting.classTree
      })
    },
    created () {
      this.classTree()
    },
    methods: {
      // 获取配置分类
      async classTree () {
        await this.$store.dispatch('setting/classTree')
      },
      // 添加配置
      onSubmit (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (Number(this.cateIds.length) <= 0) {
              notify.warning('请选择配置分类!')
              return false
            }
            this.form.category_id = this.cateIds[Number(this.cateIds.length) - 1]
            this.$store.dispatch('setting/settingAdd', this.form).then(res => {
              notify.success(res.msg)
              this.$router.push('/setting/list')
            })
          }
        })
      },
      // 返回上一页
      goBack () {
        this.$router.go(-1)
      }
    }
  }
</script>
